import { Component, OnInit, ViewChild } from '@angular/core'
import { NotificationService } from '../../shared/utils/notification.service'
import { JsonApiService } from 'app/core/api/json-api.service' //http请求配置引入
import { Router, ActivatedRoute } from '@angular/router' //路由引入
import { config } from '../../shared/smartadmin.config'

declare var $: any
@Component({
  selector: 'sa-host-assembly-test',
  templateUrl: './host-assembly-test.component.html'
})
export class HostAssemblyTestComponent implements OnInit {
  tableData: any[] = [] //table数据
  tabletotalCount: number = 0 //分页总条数
  curPage: number = 1 //分页当前页
  pageSize: number = 10 //内置的分页每页条数

  isShowAdd: boolean = false
  isShowDel: boolean = false
  isShowEdit: boolean = false

  tableSearch: any = {
    materialCode: '',
    productName: '',
    timeType: '1',
    startTime: '',
    endTime: ''
  }

  constructor(
    private jsonApiService: JsonApiService,
    private notificationService: NotificationService,
    private router: Router,
    private activatedRoute: ActivatedRoute
  ) {
    activatedRoute.queryParams.subscribe(queryParams => {})
  }
  carDeatailUseList = carTypeListObj.carDeatailUseList

  ngOnInit() {
    this.load()
    this.initBtn()
  }

  //初始化权限按钮
  initBtn() {
    var btnData = JSON.parse(decodeURIComponent(localStorage.getItem('cargpsw_supplyChain:hostAssemblyTest')))
    if (!btnData) {
      return
    }
    for (let item of btnData) {
      if (item.sourceKey === 'hostAssemblyTest:addBtn' && item.parentId) {
        this.isShowAdd = true
      }
      if (item.sourceKey === 'hostAssemblyTest:delBtn' && item.parentId) {
        this.isShowDel = true
      }
      if (item.sourceKey === 'hostAssemblyTest:editBtn' && item.parentId) {
        this.isShowEdit = true
      }
    }
  }

  // 接收子组件传递过来的数据
  getSelectTableTime(time) {
    this.tableSearch.startTime = time.startTime
    this.tableSearch.endTime = time.endTime
  }

  //table刷新
  refresh() {
    this.load()
  }
  //分页回调
  tablePaginate(e) {
    this.curPage = e.page + 1
    this.pageSize = e.rows
    this.load()
  }

  //表格搜索
  table_search() {
    this.curPage = 1
    this.load()
  }
  //初始化加载list数据
  load() {
    let data = {
      pageSize: this.pageSize,
      nowPage: this.curPage,
      parameters: {
        ...this.tableSearch
      }
    }
    let reqdata = 'gridPager=' + JSON.stringify(data)
    this.jsonApiService.http('hostAssembly/hostAssemblyPageList.html', reqdata).subscribe(data => {
      // const data = res.data
      if (data.isSuccess) {
        this.tableData = data.exhibitDatas
        this.tabletotalCount = data.recordCount
      } else {
        this.jsonApiService.message(data.message)
      }
    })
  }

  // 数据导出
  exportData() {
    let data = {
      pageSize: this.pageSize,
      nowPage: this.curPage,
      parameters: {
        ...this.tableSearch
      },
      isExport: true
    }
    let reqdata = 'gridPager=' + JSON.stringify(data)
    common.exprotDataFile(data, 'gridPager', config.BaseURL + 'hostAssembly/hostAssemblyPageList.html')
  }

  // 删除输入
  delKey(type) {
    this.tableSearch[type] = ''
  }

  //新建
  addRow() {
    this.router.navigate(['/supply-chain/smt-detection/smt-detection-page'], {
      queryParams: {
        pageType: 'new',
        type: 3,
        name: '主机组装测试',
        path: '/supply-chain/host-assembly-test',
        editUrl: 'hostAssembly/addHostAssembly.html'
      }
    })
  }
  //编辑
  editRow(row) {
    row.badInfoList = row.badList
    common.setCookie('checkedUpdata', JSON.stringify(row))
    this.router.navigate(['/supply-chain/smt-detection/smt-detection-page'], {
      queryParams: {
        pageType: 'updata',
        type: 3,
        nowPage: this.curPage,
        name: '主机组装测试',
        path: '/supply-chain/host-assembly-test',
        editUrl: 'hostAssembly/addHostAssembly.html'
      }
    })
  }
  deleteRow(row) {
    this.jsonApiService.confirm_message('此操作将永久删除该条数据，确认要删除?').then(() => {
      let reqdata = 'ids=' + row.id
      this.jsonApiService.http('hostAssembly/deleteHostAssemblyById.html', reqdata).subscribe(data => {
        this.jsonApiService.message(data.message)
        if (data.success) {
          this.load()
        }
      })
    })
  }

  //删除所选时间
  del_date() {
    this.tableSearch.startTime = ''
    this.tableSearch.endTime = ''
    $('#datetimepicker1').val('').change()
    $('#datetimepicker2').val('').change()
    $('#datetimepicker2').datetimepicker('setStartDate', null)
    $('#datetimepicker1').datetimepicker('setEndDate', null)
  }

  /**
   * 查看
   */
  typeName: string = ''
  viewDataObj: any = {
    orderNum: '',
    checkNum: '',
    badNum: '',
    badRatio: '',
    badList: []
  }
  //查看
  viewRow(row, modal, type) {
    this.viewDataObj = row
    if (type == 1) {
      this.typeName = 'SMT检测不良原因'
    } else if (type == 2) {
      this.typeName = 'PCBA烧录测试不良原因'
    } else {
      this.typeName = '主机组装测试不良原因'
    }
    modal.show()
  }
  closeViewCarModal(modal) {
    modal.hide()
  }
}
